package com.unibuy.smartdevice.ui.devs.vmc;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import androidx.recyclerview.widget.GridLayoutManager;

import com.unibuy.smartdevice.FragmentAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.databinding.FragmentVmcBinding;
import com.unibuy.smartdevice.devices.SlotField;
import com.unibuy.smartdevice.structure.BuyStructure;
import com.unibuy.smartdevice.structure.SlotStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.ToastHandlerMain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VmcFragment extends FragmentAbstract {
    public enum Option {
        TOAST(0),
        SHIPPED_PRODUCT(1),
        ADD_PRODUCT_OPEN(3),
        ADD_PRODUCT_CLOSE(4),
        ADD_PRODUCT_FREE_GIFT(5),
        ;

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }

    public static final Map<Integer, Option> optionMap = new HashMap<>();
    static {
        for (Option option : Option.values()) {
            optionMap.put(option.getOption(), option);
        }
    }

    private FragmentVmcBinding binding;
    private List<SlotStructure> slotList;
    private List<Integer> sizeList;
    private RecyclerVmcBuyListAdpter recyclerVmcBuyListAdpter;

    @Override
    protected String setName() {
        return "智販機";//智販機
    }

    @Override
    protected Context setCtx() {
        return getContext();
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                getLogs().info("commandCode:" + commandCode);
                Option option = optionMap.get(commandCode);

                switch (option) {
                    case TOAST:
                        super.execute(context, commandCode, message);
                        break;
                    case SHIPPED_PRODUCT:
                            recyclerVmcBuyListAdpter.notifyItemRangeChanged(0, slotList.size());
                        break;
                    case ADD_PRODUCT_OPEN:
                        if (!MyApp.getInstance().getBuyList().isEmpty()) {
                            sizeList.clear();
                            for (int index = 0; index < slotList.size(); index++) {
                                SlotStructure slotData = slotList.get(index);
                                int count = slotData.getCount();
                                sizeList.add(index, count);

                                for (int i = 0; i < MyApp.getInstance().getBuyList().size(); i++) {
                                    BuyStructure buyData = MyApp.getInstance().getBuyList().get(i);
                                    if (buyData.getField() == slotData.getField() && buyData.getSlot() == slotData.getSlot()) {
                                        int slot = buyData.getSlot();
                                        int count2 = buyData.getCount();
                                        count = count - count2;
                                    }
                                }

                                slotData.setCount(count);

                                recyclerVmcBuyListAdpter.notifyItemChanged(index);
                            }
                        }
                        break;
                    case ADD_PRODUCT_CLOSE:
                        if (!sizeList.isEmpty()) {
                            for (int index = 0; index < slotList.size(); index++) {
                                int count = sizeList.get(index);

                                SlotStructure slotData = slotList.get(index);
                                slotData.setCount(count);

                                recyclerVmcBuyListAdpter.notifyItemChanged(index);
                            }
                            sizeList.clear();
                        }
                        break;
                }
            }
        };
    }

    public VmcFragment() {
        super(null);
    }

    public VmcFragment(HandlerMain srcHandlerMain) {
        super(srcHandlerMain);
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        VmcViewModel vmcViewModel = new ViewModelProvider(this).get(VmcViewModel.class);
        binding = FragmentVmcBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        slotList = new ArrayList<>();
        sizeList = new ArrayList<>();

        recyclerVmcBuyListAdpter = new RecyclerVmcBuyListAdpter(this, slotList);
        binding.recyclerBuy.setAdapter(recyclerVmcBuyListAdpter);
        binding.recyclerBuy.setLayoutManager(new GridLayoutManager(getContext(), 3));

        for (int i = 0; i < MyApp.getInstance().getSlotList(SlotField.VMC.getField()).size(); i++) {
            SlotStructure slot = MyApp.getInstance().getSlotList(SlotField.VMC.getField()).get(i);
            if (!slot.getProduct().getProductID().isEmpty() && !slot.getProduct().getProductName().isEmpty() && slot.getProduct().getSellingPrice() > 0) {
                slotList.add(slot);
            }
        }

        if (!slotList.isEmpty()) {
            recyclerVmcBuyListAdpter.notifyItemInserted(slotList.size() - 1);
        }

        if (isUseStartOnSwitchFragment()) {
            startOnSwitchFragment();
            setUseStartOnSwitchFragment(false);
        }

        return root;
    }

    @Override
    public void startOnSwitchFragment() {

    }

    @Override
    public void stopOnSwitchFragment() {

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}